package com.cheer.ba.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.OilGoodsInfoModel;
import com.cheer.ba.model.ProvinceModel;
import com.cheer.ba.presenter.impl.OilMealPresenter;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.fragment.OilMenuFrg;
import com.cheer.pickerview.Util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 送油批发
 */
public class FindOilAty extends BaseCommAty {

    //侧滑栏目
    @BindView(R.id.id_drawer_layout)
    DrawerLayout mDrawerLayout;
    //右侧
    @BindView(R.id.id_drawer_right_linear)
    LinearLayout mRightView;
    //地区
    @BindView(R.id.id_drawer_oil_area_txt)
    TextView mTxtOilArea;
    private String mOilArea = "湖北省";
    private ArrayList<String> mProvinces = new ArrayList<String>();
    //油品种类
    @BindView(R.id.id_drawer_oil_type_txt)
    TextView mTxtOilType;
    private String mOilType = "1";
    //油源
    @BindView(R.id.id_drawer_oil_original_txt)
    TextView mTxtOilOriginal;
    private String mOilOriginal = "-1";
    private String mOilOriginalName = "全部";
    private List<OilGoodsInfoModel> mOilGoods = new ArrayList<OilGoodsInfoModel>();
    private ArrayList<String> mOilOriginals = new ArrayList<String>();
    //全部
    @BindView(R.id.id_drawer_oil_all_txt)
    TextView mTxtOilRuleAll;
    //国4
    @BindView(R.id.id_drawer_oil_rule_5_txt)
    TextView mTxtOilRule5;
    //国五
    @BindView(R.id.id_drawer_oil_rule_nor_txt)
    TextView mTxtOilRuleNor;
    //标准
    private String mOilRule = "-1";

    //右侧菜单状态
    private boolean isDirection_right = false;

    public static void startFindOilAty(Activity activity, String mArea, String oilType, String oilOriginal, String oilRule) {
        Intent intent = new Intent(activity, FindOilAty.class);
        intent.putExtra(IntentKey.FindOil.OIL_AREA, mArea);
        intent.putExtra(IntentKey.FindOil.OIL_TYPE, oilType);
        intent.putExtra(IntentKey.FindOil.OIL_ORIGINAL, oilOriginal);
        intent.putExtra(IntentKey.FindOil.OIL_RULE, oilRule);
        activity.startActivity(intent);
    }

    @Override
    public void initPresenter() {
        presenter = new OilMealPresenter();
    }

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_find_oil, null, false));
    }

    @Override
    public void initView() {
        setTitleText("柴油批发");
//        int width = SystemUtils.getScreenWidth(aty) - DensityUtils.dip2px(aty, 100);
//        DrawerLayout.LayoutParams params = new DrawerLayout.LayoutParams(width, DrawerLayout.LayoutParams.MATCH_PARENT);
//        mRightView.setLayoutParams(params);

//        mOilArea = getIntent().getStringExtra(IntentKey.FindOil.OIL_AREA);
//        mOilType = getIntent().getStringExtra(IntentKey.FindOil.OIL_TYPE);
//        mOilOriginal = getIntent().getStringExtra(IntentKey.FindOil.OIL_ORIGINAL);
//        mOilRule = getIntent().getStringExtra(IntentKey.FindOil.OIL_RULE);

        setTitleIcon(R.drawable.ic_search_white, false, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //搜索
                Intent intent = new Intent(aty, SearchOilAty.class);
                intent.putExtra(IntentKey.General.KEY_PAGE, IntentCode.General.JUMP_FINDOIL);
                showActivity(aty, intent);
            }
        });
        ((OilMealPresenter) presenter).getOilType();
    }

    @Override
    public void initData() {
        //设置抽屉打开时，主要内容区被自定义阴影覆盖
        mDrawerLayout.setDrawerShadow(R.color.translucent, GravityCompat.START);
        mDrawerLayout.setDrawerListener(new DrawerLayoutStateListener());
        refreshFrg();
        //获得省级数据
        final ArrayList<ProvinceModel> provinces = (ArrayList<ProvinceModel>) ((OilMealPresenter) presenter).getData(4, false);
        if (null != provinces && !provinces.isEmpty()) {
            mProvinces.clear();
            for (ProvinceModel p : provinces) {
                mProvinces.add(p.mProvinceName);
            }
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != tag) {
            if (TextUtils.equals(tag, ConstantTag.GET_OIL_TYPE_LIST)) {
                List<OilGoodsInfoModel> mOilTypes = (List<OilGoodsInfoModel>) t;
                if (null != mOilTypes && !mOilTypes.isEmpty()) {
                    mOilGoods.clear();
                    mOilOriginals.clear();
                    OilGoodsInfoModel model = new OilGoodsInfoModel();
                    model.name = "全部";
                    model.uid = "-1";
                    mOilGoods.add(model);
                    mOilGoods.addAll(mOilTypes);
                    for (OilGoodsInfoModel goods : mOilGoods) {
                        mOilOriginals.add(goods.name);
                    }
                }
            }
        }
    }

    public void showDrawer() {
        mDrawerLayout.openDrawer(mRightView);
    }

    /**
     * 刷新内容区
     */
    public void refreshFrg() {
        OilMenuFrg mOilMenuFrg = OilMenuFrg.newInstance(mOilArea, mOilType, mOilOriginal, mOilRule);
        mOilMenuFrg.setFindOilAty(this);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.id_oil_content_view, mOilMenuFrg).commit();
        mDrawerLayout.closeDrawer(mRightView);
    }

    /**
     * 右侧文字
     */
    public void setDrawerRight() {
        mTxtOilArea.setText(mOilArea);
        mTxtOilType.setText("柴油");
        mOilType = "1";
        mTxtOilOriginal.setText(mOilOriginalName);

        mTxtOilRuleAll.setSelected(false);
        mTxtOilRule5.setSelected(false);
        mTxtOilRuleNor.setSelected(false);

        if (TextUtils.equals("-1", mOilRule)) {
            mTxtOilRuleAll.setSelected(true);
        } else if (TextUtils.equals("2", mOilRule)) {
            mTxtOilRuleNor.setSelected(true);
        } else if (TextUtils.equals("1", mOilRule)) {
            mTxtOilRule5.setSelected(true);
        }
    }

    /**
     * DrawerLayout状态变化监听
     */
    public class DrawerLayoutStateListener extends DrawerLayout.SimpleDrawerListener {
        /**
         * 当导航菜单滑动的时候被执行
         */
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            setDrawerRight();
            super.onDrawerSlide(drawerView, slideOffset);
        }

        /**
         * 当导航菜单执行打开时
         */
        @Override
        public void onDrawerOpened(View drawerView) {
            if (drawerView == mRightView) {
                isDirection_right = true;
            }
            super.onDrawerOpened(drawerView);
        }

        /**
         * 当导航菜单关闭时执行
         */
        @Override
        public void onDrawerClosed(View drawerView) {
            if (drawerView == mRightView) {
                isDirection_right = false;
            }
            super.onDrawerClosed(drawerView);
        }
    }

    @OnClick({R.id.id_drawer_oil_location_rela, R.id.id_drawer_oil_original_rela, R.id.id_drawer_oil_all_txt, R.id.id_drawer_oil_rule_5_txt, R.id.id_drawer_oil_rule_nor_txt, R.id.id_close_drawer_txt, R.id.id_choose_btn})
    public void onClick(View mView) {
        switch (mView.getId()) {
            //地区
            case R.id.id_drawer_oil_location_rela:
                Util.alertBottomWheelOption(aty, mProvinces, new Util.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int postion) {
                        mOilArea = mProvinces.get(postion);
                        mTxtOilArea.setText(mOilArea);
                    }
                }, 16);
                break;
            //油源
            case R.id.id_drawer_oil_original_rela:
                if (mOilOriginals.isEmpty()) {
                    return;
                }
                Util.alertBottomWheelOption(aty, mOilOriginals, new Util.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int postion) {
                        mOilOriginalName = mOilOriginals.get(postion);
                        mTxtOilOriginal.setText(mOilOriginalName);
                        if (!mOilGoods.isEmpty()) {
                            mOilOriginal = mOilGoods.get(postion).uid;
                        }
                    }
                }, 0);
                break;
            //全部
            case R.id.id_drawer_oil_all_txt:
                mTxtOilRuleAll.setSelected(true);
                mTxtOilRule5.setSelected(false);
                mTxtOilRuleNor.setSelected(false);
                mOilRule = "-1";
                break;
            //国4
            case R.id.id_drawer_oil_rule_5_txt:
                mTxtOilRuleAll.setSelected(false);
                mTxtOilRule5.setSelected(true);
                mTxtOilRuleNor.setSelected(false);
                mOilRule = "1";
                break;
            //普柴
            case R.id.id_drawer_oil_rule_nor_txt:
                mTxtOilRuleAll.setSelected(false);
                mTxtOilRule5.setSelected(false);
                mTxtOilRuleNor.setSelected(true);
                mOilRule = "2";
                break;
            //关闭
            case R.id.id_close_drawer_txt:
                mDrawerLayout.closeDrawer(mRightView);
                break;
            //刷选,确定
            case R.id.id_choose_btn:
                if (!isDirection_right) {
                    mDrawerLayout.openDrawer(mRightView);
                } else {
                    mDrawerLayout.closeDrawer(mRightView);
                    refreshFrg();
                }
                break;
        }
    }

    /**
     * 根据onPostCreate回调的状态，还原对应的icon_state
     */
    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }

    /**
     * 根据onSaveInstanceState回调的状态，保存当前icon state
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
}
